# coding:utf-8
# writingtime:2022-8-5
# reference: https://doi.org/10.1007/s40815-021-01243-2

from DistanceFunction.euclidean import Euclidean


class ZLF:
    def __init__(self, dataList, membershipMatrix, clusterCenter):
        """
        function
        :param dataList: 样本向量
        :param membershipMatrix: 关系矩阵
        :param clusterCenter: 聚合中心
        """
        self.dataList = dataList
        self.membershipMatrix = membershipMatrix
        self.clusterCenter = clusterCenter

    def getcomp(self):
        """
        function：计算上半部分，comp
        :return:
        """
        sum1 = 0
        temp = [[r[col] for r in self.membershipMatrix] for col in range(len(self.membershipMatrix[0]))]
        for j in range(len(self.dataList)):
            sum2 = 0
            for i in range(len(self.clusterCenter)):
                sum2 += Euclidean.getresult(self.dataList[j], self.clusterCenter[i])
            sum1 += (1 - max(temp[j])) / sum2
        return sum1

    def getsep(self):
        """
        function: 计算下班部分，sep
        :return:
        """
        sum1 = 0
        for i in range(len(self.clusterCenter)):
            for k in range(len(self.clusterCenter)):
                if i != k:
                    sum1 += Euclidean.getresult(self.clusterCenter[i], self.clusterCenter[k])
        sum1 /= (len(self.clusterCenter) * (len(self.clusterCenter) - 1)) / 2
        return sum1

    def getzlf(self):
        """
        function: 计算zlf评价
        :return:
        """
        return self.getcomp()/self.getsep()

    @staticmethod
    def getresult(dataList, membershipMatrix, clusterCenter, m=2, a=2):
        """
        function: zlf评价函数
        :param dataList: 样本向量
        :param membershipMatrix: 评价矩阵
        :param clusterCenter: 中心点向量
        :return: FM评价值
        """
        return ZLF(dataList, membershipMatrix, clusterCenter).getzlf()


